var form
$(function(){
    layui.use(['layer', 'form'], function(){
        form = layui.form();
        form.on('checkbox(isPriceException)', function (data) {
            if (data.elem.checked) {
                $("#isPriceException").val("1");
                $("#exception").show();
            } else {
                $("#exception").hide();
                $("#isPriceException").val("0");
            }
        });
        init();
    });
})
function init(){
    $("#btn").click(function(){
        submitOrder();
    })
    doubleClick("WU_FILE_0",function(obj){
        $(obj).remove()
        $("#filePicker").show();
    })
    doubleClick("WU_FILE_1",function(obj){
        $(obj).remove()
        $("#filePicker").show();
    })
    setInterval(function(){
        if($("#actPrice").val()!= '' && $("#actProductNum").val()!=''){
            var total = parseInt($("#actProductNum").val()) * parseFloat($("#actPrice").val());
            if(!isNaN(total)){
                $("#productTotalPrice").val(total)
            }
            var orderTotal = parseFloat($("#productTotalPrice").val()) + parseFloat($("#chargeFee").val())
                + parseFloat($("#serviceFee").val());
            if(!isNaN(orderTotal)){
                $("#totalPrice").val(orderTotal);
            }
        }
    },1000)
    $.ajax({
        url:server_url + '/rest/staff/orderDetail.do',
        data:{orderId:params()['orderId'],ran:Math.random()},
        success:function(data){
            console.log(data)
            var order = data.data
            $("#orderCode").text(order.orderCode);
            $("#productName").text(order.lstOrderDetail[0].productName)
            $("#spec").text(order.lstOrderDetail[0].spec)
            $("#areaName").text(order.lstOrderDetail[0].areaName)
            $("#addTime").text(order.addTime)
            $("#buyer").val(order.consignee)
            $("#seller").val(order.lstOrderDetail[0].supplyName);
        }
    })
}
function submitOrder(){
    if($("#actPrice").val() == ""){
        easyAlert("请输入成交单价")
        return;
    }
    if(!isPriceNumber($("#actPrice").val())){
        easyAlert("成交单价只能输入数字和小数点")
        return;
    }
    if($("#chargeFee").val() == ""){
        easyAlert("请输入装卸费")
        return;
    }
    if(!isPriceNumber($("#chargeFee").val())){
        easyAlert("装卸费只能输入数字和小数点")
        return;
    }
    if($("#actProductNum").val() == ""){
        easyAlert("请输入成交数量")
        return;
    }
    if(!isInt($("#actProductNum").val())){
        easyAlert("数量只能输入数字")
        return;
    }
    if($("#serviceFee").val() == ""){
        easyAlert("请输入服务费")
        return;
    }
    if(!isPriceNumber($("#serviceFee").val())){
        easyAlert("服务费只能输入数字和小数点")
        return;
    }
    if($("#payToSeller").val() == ""){
        easyAlert("请输入向卖家打款数")
        return;
    }
    if(!isPriceNumber($("#payToSeller").val())){
        easyAlert("向卖家打款数只能输入数字和小数点")
        return;
    }
    if($("#orderManager").val() == ""){
        easyAlert("请输入订单负责人")
        return;
    }
    if($("#salesPerson").val() == ""){
        easyAlert("请输入货源负责人")
        return;
    }
    if( $("#isPriceException").val() == '1'){
        if($("#priceExceptionReason").val() == ''){
            easyAlert("请输入价格异常原因");
            return;
        }
    }
    $.ajax({
        url:server_url+"/rest/staff/updateOrder.do?userId="+userid+"&id="+params()['orderId']+"&paidImgUrl1="+$("#WU_FILE_0").attr("src")+"&paidImgUrl2="+$("#WU_FILE_1").attr("src")+"&ran="+Math.random(),
        data:$("form").serialize(),
        success:function(data){
            easyAlert("处理成功")
            window.location.href = '/LianniuWeb/staff/orders.do';
        }
    })
}